<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="办事标题" prop="affairTitle">
        <el-input
          v-model="queryParams.affairTitle"
          placeholder="请输入办事标题"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办事人" prop="affairAuthor">
        <el-input
          v-model="queryParams.affairAuthor"
          placeholder="请输入办事人"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="isDisplay">
        <el-select v-model="queryParams.isDisplay" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in displayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否置顶" prop="isTop">
        <el-select v-model="queryParams.isTop" placeholder="请选择" clearable>
          <el-option
            v-for="dict in topOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="办事类型" prop="affairType">
        <el-select v-model="queryParams.affairType" placeholder="请选择办事类型" clearable
                   size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:affair:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:affair:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:affair:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="affairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" prop="affairId" key="affairId" v-if="columns[0].visible"/>
      <el-table-column label="办事内容" align="center" prop="affairDetails" :show-overflow-tooltip='true' key="affairDetails" v-if="columns[1].visible">
        <template slot-scope="scope">
          <div v-html="scope.row.affairDetails"></div>
        </template>
      </el-table-column>
      <el-table-column label="办事标题" align="center" prop="affairTitle" :show-overflow-tooltip='true' key="affairTitle" v-if="columns[2].visible"/>
      <el-table-column label="办事人" align="center" prop="affairAuthor" :show-overflow-tooltip='true' key="affairAuthor" v-if="columns[3].visible"/>
      <el-table-column label="是否显示" align="center" prop="isDisplay" :formatter="displayFormat" :show-overflow-tooltip='true' key="isDisplay" v-if="columns[4].visible"/>
      <el-table-column label="是否置顶" align="center" prop="isTop" :formatter="isTopFormat" :show-overflow-tooltip='true' key="isTop" v-if="columns[5].visible"/>
      <el-table-column label="办事类型" align="center" prop="affairType"  :formatter="affairTypeFormat" :show-overflow-tooltip='true' key="affairType" v-if="columns[6].visible"/>
      <el-table-column label="发布时间" align="center" prop="affairaffairDatetime" width="180" :show-overflow-tooltip='true' key="affairaffairDatetime" v-if="columns[7].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.affairaffairDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点击次数" align="center" prop="contentHit"  :show-overflow-tooltip='true' key="contentHit" v-if="columns[8].visible"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip='true' key="remark" v-if="columns[9].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:affair:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:affair:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运营管理办事指南对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="办事标题" prop="affairTitle">
          <el-input v-model="form.affairTitle" placeholder="请输入办事标题"/>
        </el-form-item>
        <el-form-item label="办事内容" prop="affairDetails">
          <editor v-model="form.affairDetails" :min-height="192"/>
        </el-form-item>
        <el-form-item label="办事人" prop="affairAuthor">
          <el-input v-model="form.affairAuthor" placeholder="请输入办事人"/>
        </el-form-item>
        <el-form-item label="是否显示" prop="isDisplay">
          <el-select v-model="form.isDisplay" placeholder="请选择" style="width: 680px">
            <el-option
              v-for="dict in displayOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-select v-model="form.isTop" placeholder="请选择" style="width: 680px">
            <el-option
              v-for="dict in topOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="办事类型" prop="affairType">
          <el-select v-model="form.affairType" placeholder="请选择办事类型" style="width: 680px">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="affairaffairDatetime">
          <el-date-picker clearable size="small"
                          v-model="form.affairaffairDatetime"
                          type="date"
                          style="width: 680px"
                          value-format="yyyy-MM-dd"
                          placeholder="选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listAffair,
    getAffair,
    delAffair,
    addAffair,
    updateAffair,
    exportAffair
  } from '@/api/server/operate/affair'
  import Editor from "@/components/Editor";

  export default {
    name: 'Affair',
    components: {Editor},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 运营管理办事指南表格数据
        affairList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        typeOptions:[],
        //显示状态字典
        displayOptions: [],
        //置顶状态字典
        topOptions: [],
        //日期范围
        dateRange:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          affairDetails: null,
          affairTitle: null,
          affairAuthor: null,
          isDisplay: null,
          isTop: null,
          affairType: null,
          contentHit: null,
          dateRange:null,
        },
        // 表单参数
        form: {}
        ,
        queryForm: {
          affairAuthor: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        },
        //列信息
        columns: [
          {key: 0, label: "序号", visible: true},
          {key: 1, label: "办事内容", visible: true},
          {key: 2, label: "办事标题", visible: true},
          {key: 3, label: "办事人", visible: true},
          {key: 4, label: "是否显示", visible: true},
          {key: 5, label: "是否置顶", visible: true},
          {key: 6, label: "办事类型", visible: true},
          {key: 7, label: "发布时间", visible: true},
          {key: 8, label: "点击次数", visible: true},
          {key: 9, label: "备注", visible: true},
        ],
        // 表单校验
        rules: {
          isTop: [{
            required: true, message: '是否置顶不能为空', trigger: 'change'
          }
          ],
          affairDetails: [{
            required: true,
            message: "办事内容不能为空",
            trigger: "blur"
          }],
          affairTitle: [{
            required: true,
            message: "办事标题不能为空",
            trigger: "blur"
          }],
          affairAuthor: [{
            required: true,
            message: "办事人不能为空",
            trigger: "blur"
          }],
          isDisplay: [{
            required: true,
            message: "是否显示不能为空",
            trigger: "change"
          }],
          affairType: [{
            required: true,
            message: "办事类型不能为空",
            trigger: "change"
          }],
          affairaffairDatetime: [{
            require:true,
            message:"发布时间不能为空",
            trigger:"blur"
          }],
        },
      }
    },
    created() {
      this.getList()
      this.getDicts('t_affair_type').then(response => {
        this.typeOptions = response.data;
      })
      this.getDicts('t_is_display').then(response => {
        this.displayOptions = response.data;
      })
      this.getDicts('t_is_top').then(response => {
        this.topOptions = response.data;
      })
    },
    methods: {
      /** 查询运营管理办事指南列表 */
      getList() {
        this.loading = true
        listAffair(this.addDateRange1(this.queryParams, this.dateRange)).then(response => {
          this.affairList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      affairTypeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.affairType)
      },
      isTopFormat(row, column) {
        return this.selectDictLabel(this.topOptions, row.isTop)
      },
      displayFormat(row, column) {
        return this.selectDictLabel(this.displayOptions, row.isDisplay)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          affairId: null,
          affairDetails: null,
          affairTitle: null,
          affairAuthor: null,
          isDisplay: null,
          isTop: null,
          affairType: null,
          affairaffairDatetime: null,
          contentHit: null,
          createBy: null,
          delFlag: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }

        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.$refs['queryForm'].validate(valid => {
          if (valid) {
            this.getList()
          }
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = '';
        this.queryParams.params.beginTime = ''
        this.queryParams.params.endTime = ''
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.affairId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加运营管理办事指南'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const affairId =
          row.affairId || this.ids
        getAffair(affairId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改运营管理办事指南'
        })


      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
            if (valid) {
              if (this.form.affairId != null) {
                updateAffair(this.form).then(response => {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                })

              } else {
                addAffair(this.form).then(response => {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                })

              }
            }
          }
        )

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const affairIds = row.affairId || this.ids
        this.$confirm('是否确认删除该数据项?', '警告', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'warning'
        }).then(function() {
          return delAffair(affairIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有运营管理办事指南数据项?', '提示', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'info'
        }).then(function() {
          return exportAffair(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
